﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.BaseForm.Properties;
using Slippor.Douban.Radio.Tracker;

namespace Slippor.Douban.Radio.BaseForm
{
    public class ToolStripTracker : ITracker
    {
        public const string Key = "ToolStrip";
        private int _index;
        private ToolStripMenuItem _currentHistoryItem = new ToolStripMenuItem
                                                            {
                                                                Tag = Song.DefaultSong,
                                                            };
        private Dictionary<Song, ToolStripItem> _historyItems = new Dictionary<Song, ToolStripItem>();

        public ToolStripMenuItem CurrentHistoryItem
        {
            get { return _currentHistoryItem; }
        }

        private static object _forLock = new object();
        #region ITracker Members
        public string Name
        {
            get { return Key; }
        }

        public void Track(Song song)
        {
            lock(_forLock)
            {
                Song currentSong = _currentHistoryItem.Tag as Song;
                if (song != currentSong)
                {
                    if (currentSong != Song.DefaultSong)
                    {
                        _currentHistoryItem.BackColor = Color.Transparent;
                    }
                    var historyItem = new ToolStripMenuItem(RadioUtility.VerifyText(String.Format("({2}) {1}. {0}", song.Title, ++_index, song.ListenTime.ToShortTimeString())))
                    {
                        Tag = song,
                        BackColor = Color.LightSkyBlue,
                        ToolTipText = StringResource.ClickToCopy,
                        ForeColor = Color.DarkGray,
                        Image = song.FaveState == FavState.Fav ? ImageCache.Fav : ImageCache.UnFav
                    };
                    _historyItems.Add(song, historyItem);
                    _currentHistoryItem = historyItem;
                }
            }
        }

        public void TrackUpdate(ICommand command, Song song)
        {
            lock (_forLock)
            {
                if (song != null && _historyItems.ContainsKey(song))
                {
                    ToolStripItem item = _historyItems[song];
                    switch (command.Command)
                    {
                        case Command.Fav:
                            switch (song.FaveState)
                            {
                                case FavState.Fav:
                                    TrackUpdate(ImageCache.Fav, item);
                                    break;
                                case FavState.UnFav:
                                    TrackUpdate(ImageCache.UnFav, item);
                                    break;
                                default:
                                    break;
                            }
                            break;
                        case Command.Filter:
                            TrackUpdate(Resources.Filter, item);
                            break;
                        case Command.Next:
                            TrackUpdate(Resources.Next, item);
                            break;
                        case Command.Del:
                            TrackUpdate(Resources.Del, item);
                            break;
                        case Command.Other:
                            if (song.Album != null)
                            {
                                item.Text = RadioUtility.VerifyText(String.Format("({0}) {1}. {2} - {3}",
                                                                         song.ListenTime.ToShortTimeString(), _index,
                                                                         song.Title, song.Album.ArtistString));
                                item.ForeColor = Color.Black;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        public void Clear()
        {
            _index = 0;
            _historyItems.Clear();
            _currentHistoryItem = new ToolStripMenuItem { Tag = Song.DefaultSong };
        }

        private void TrackUpdate(Bitmap icon,ToolStripItem item)
        {
            item.Image = icon;
        }

        public void Stop()
        {
            ;
        }

        public void Pause()
        {
        }

        public void Resume()
        {
        }
        #endregion
    }
}